#include <iostream>
#include <string.h>
#include <stdlib.h>

class small_block
{
    public:
        char *        cur_usable_buffer;  //指向该bolck的可用buff首地址
        char *        buffer_end;         //指向该block的buff尾地址
        small_block * next_block;         //指向下一个bolok
        int           no_enough_times;    //每次分配内存都会顺着small_block链，寻找是否有足够分配的内存，如果内找到，则该值+1   
};

class big_block
{
    public:
        char *        big_buffer;         //大内存buff首地址
        big_block *   next_block;         //指向下一个bolok
};

class memory_pool
{
    public:
        size_t        small_buff_capacity;         //small_buff的容量
        small_block * cur_usable_small_block;      //每次分配小内存从该指针向small_block找
        big_block *   big_block_start;             //指向big_bolck链表头
        small_block   small_block_start[0];        //指向small_block链表头,柔性数组，该类的成员函数必须使用静态函数，这样保证其位置定义在整个类的内存末端
        static memory_pool * createPool(size_t capacity);
        static void          destroyPool(memory_pool * pool);
        static char *        createNewSmallBlock(memory_pool * pool , size_t size);
        static char *        mallocBigBlock(memory_pool * pool , size_t size);
        static void *        poolMalloc(memory_pool * pool , size_t size);
        static void          freeBigBlock(memory_pool * pool , char * buffer_ptr);
};
